package org.ccams.service;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.View;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;

public class CcamsWebServiceController implements Controller {
	private static Logger logger = Logger
			.getLogger(CcamsWebServiceController.class);

	/*
	 * 通过trancode传入参数，返回参数对应的交易
	 */
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String trancode = request.getParameter("trancode");
		logger.info("开始执行交易，交易码: " + trancode);
		Class<?> tranClass = getTranClass(trancode);
		if (tranClass != null) {
			return new ModelAndView((View) tranClass.newInstance(), null);
		}
		return null;
	}

	/*
	 * 取交易代码所在的类
	 */
	public Class<?> getTranClass(String trancode) throws Exception {
		try {
			return Class.forName("org.ccams.tran.Tran" + trancode);
		} catch (ClassNotFoundException e) {
			logger.info("交易类型未定义，交易码：" + trancode);
			return Class.forName("org.ccams.tran.TranNull");
		} catch (Exception e) {
			logger.info("交易类型未定义，交易码：" + trancode);
			return null;
		}
	}

}